var material_model = null;
var updateGuid = null;
var userall = null;     //人员
var mk_content = "";		//	材料详情
var material_guid = "";	//	材料ID组合
var material_count = "";	//	数量组合
var content;

$(function () {
    getuserall();
    $.when(
        vm.inte()
    ).done(
        $("#jqGrid").jqGrid({
            url: baseURL + 'bss/dmaterialmodel/list',
            datatype: "json",
            colModel: [
                { hidden:true ,label: 'guid', name: 'guid', index: 'guid', width: 50, key: true },
                { label: '版本', name: 'queryedition', index: 'queryedition', width: 80 },
                { label: '类型', name: 'querytype', index: 'querytype', width: 80 },
                { label: '名称', name: 'name', index: 'name', width: 80 },
                { label: '详情', name: 'content', index: 'content', width: 300 },
                { label: '创建人', name: 'createId', index: 'create_id', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in userall) {
                            if (cellvalue == userall[d].userId) {
                                return userall[d].username;
                            }
                        }
                    }
                },
                { hidden:true ,label: '数据状态', name: 'datastate', index: 'datastate', width: 80 },
                { label: '操作', name: '', width: 80,formatter: function (cellvalue, options, rowObject) {
                        return  "<a  class='btn btn-primary' onclick='updataContent(\"" + rowObject.guid + "\")' >&nbsp;修改详情</a>"
                    }
                }
            ],
            viewrecords: true,
            height: 385,
            rowNum: 10,
            rowList : [10,30,50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth:true,
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader : {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames : {
                page:"page",
                rows:"limit",
                order: "order"
            },
            gridComplete:function(){
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            }
        })
	)
});


//  修改详情
function updataContent(guid) {

    var title ="已选择材料";
    var content = "dmaterialmodelMiddle.html?modelGuid="+guid;
    layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.5,
        maxmin: true, //开启最大化最小化按钮
        area: ['100%', '100%'],
        content: content

    });

}

//  适用范围
function getRange() {

}


function  getuserall()
{
    $.get(baseURL + "sys/user/select" , function (r) {
        userall = r.returnlist;
    });
}




var vm = new Vue({
	el:'#rrapp',
	data:{

        gtshow:false,
        byqshow:false,
        tjdshow:false,
        dslxshow:false,
        xhhdshow:false,
         qdshow:false,
		showList: true,

		title: null,
        q:{
            mtype:null,
            mname:null,
            edition:null
        },
		dMaterialModel: {},
        material_model:[],
        voltage_type:[],
        gt_height:[],
        gt_sj:[],
        line_type:[],
        gt_count:[],
        edition_type:[],
        model_type:[],
        byq_type:[],
        byq_rl:[],
        byq_gz:[],
        byq_cxs:[],
        byq_ylx:[],
        tjd_kgfa:[],
        tjd_kgzj:[],
        qd_type:[],
        qd_fs:[],
        gt_type:[],
        gt_material:[]

	},
	methods: {

        inte: function () {
            //模块类型
            $.get(baseURL + "api/bdatadictionary/select?d_code=material_model", function(r){
               vm.material_model=r.returnlist;
            });
             // 电压等级
            $.get(baseURL + "api/bdatadictionary/select?d_code=voltage_type", function(r){
                vm.voltage_type=r.returnlist;
            });
             //杆高
            $.get(baseURL + "api/bdatadictionary/select?d_code=gt_height", function(r){
                vm.gt_height=r.returnlist;
            });
            //稍径
            $.get(baseURL + "api/bdatadictionary/select?d_code=gt_sj", function(r){
                vm.gt_sj=r.returnlist;
            });
            //杆型
            $.get(baseURL + "api/bdatadictionary/select?d_code=gt_type", function(r){
                vm.gt_type=r.returnlist;
            });
            //导线类型
            $.get(baseURL + "api/bdatadictionary/select?d_code=line_type", function(r){
                vm.line_type=r.returnlist;
            });
            //杆塔杆数，变压器杆数
            $.get(baseURL + "api/bdatadictionary/select?d_code=gt_count", function(r){
                vm.gt_count=r.returnlist;
            });
            //典设版本
            $.get(baseURL + "api/bdatadictionary/select?d_code=edition_type", function(r){
                vm.edition_type=r.returnlist;
            });
            //模块大类
            $.get(baseURL + "api/bdatadictionary/select?d_code=model_type", function(r){
                vm.model_type=r.returnlist;
            });

            //变压器类型
            $.get(baseURL + "api/bdatadictionary/select?d_code=byq_type", function(r){
                vm.byq_type=r.returnlist;
            });
            //变压器容量
            $.get(baseURL + "api/bdatadictionary/select?d_code=byq_rl", function(r){
                vm.byq_rl=r.returnlist;
            });
            // 变压器杆质
            $.get(baseURL + "api/bdatadictionary/select?d_code=byq_gz", function(r){
                vm.byq_gz=r.returnlist;
            });
            // 变压器低压出线
            $.get(baseURL + "api/bdatadictionary/select?d_code=byq_cxs", function(r){
                vm.byq_cxs=r.returnlist;
            });
            // 变压器引流线
            $.get(baseURL + "api/bdatadictionary/select?d_code=byq_ylx", function(r){
                vm.byq_ylx=r.returnlist;
            });
            // T接点开关支架
            $.get(baseURL + "api/bdatadictionary/select?d_code=tjd_kgzj", function(r){
                vm.tjd_kgzj=r.returnlist;
            });
            // T接点开关方案
            $.get(baseURL + "api/bdatadictionary/select?d_code=tjd_kgfa", function(r){
                vm.tjd_kgfa=r.returnlist;
            });
            //(墙担)布线方式
            $.get(baseURL + "api/bdatadictionary/select?d_code=qd_fs", function(r){
                vm.qd_fs=r.returnlist;
            });
            // (墙担)墙架点类型
            $.get(baseURL + "api/bdatadictionary/select?d_code=qd_type", function(r){
                vm.qd_type=r.returnlist;
            });
            // (杆塔)材质
            $.get(baseURL + "api/bdatadictionary/select?d_code=gt_material", function(r){
                vm.gt_material=r.returnlist;
            });


        },
		query: function () {
			vm.reload();
		},
		add: function(){
            vm.gtshow=false,
            vm.byqshow=false,
            vm.tjdshow=false,
             vm.dslxshow=false,
             vm.xhhdshow=false;
            vm.qdshow=false;
            vm.showList = false;
			vm.title = "新增";
			vm.dMaterialModel = {};
		},
		update: function (event) {
			var guid = getSelectedRow();
			if(guid == null){
				return ;
			}

            vm.gtshow=false,
            vm.byqshow=false,
            vm.tjdshow=false,
            vm.dslxshow=false,
            vm.xhhdshow=false;
            vm.qdshow=false;
			vm.showList = false;
            vm.title = "修改";
            updateGuid = guid;
            vm.getInfo(guid)
		},
		saveOrUpdate: function (event) {
			var url = vm.dMaterialModel.guid == null ? "bss/dmaterialmodel/save" : "bss/dmaterialmodel/update";
			if(vm.dMaterialModel.guid != null){
			    // console.log("修改=="+content)
                vm.dMaterialModel.content = content;
            }else{
                vm.dMaterialModel.content = mk_content;
                // console.log("新增")
            }
            material_guid = material_guid.substr(0,material_guid.length);
            material_count = material_count.substr(0,material_count.length);
            vm.dMaterialModel.materialGuid = material_guid;
            vm.dMaterialModel.materialCount = material_count;
            mk_content = "";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.dMaterialModel),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "bss/dmaterialmodel/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "bss/dmaterialmodel/info/"+guid, function(r){
                vm.dMaterialModel = r.dMaterialModel;
                if(vm.dMaterialModel.querytype=="杆塔")
                {
                    vm.gtshow=true;
                }
                if(vm.dMaterialModel.querytype=="变压器")
                {
                    vm.byqshow=true;
                }
                if(vm.dMaterialModel.querytype=="T接点")
                {
                    vm.tjdshow=true;
                }
                if(vm.dMaterialModel.querytype=="典设拉线")
                {
                    vm.dslxshow=true;
                }
                if(vm.dMaterialModel.querytype=="下户横担")
                {
                    vm.xhhdshow=true;
                }
                if(vm.dMaterialModel.querytype=="墙担")
                {
                    vm.qdshow=true;
                }

                content = r.dMaterialModel.content;
            });
		},
		reload: function (event) {
			vm.showList = true;

			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'mtype': vm.q.mtype,
                    'mname': vm.q.mname,
                    'edition': vm.q.edition,

                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});